class_name InteractiveItem
extends Area2D

#region 属性
@export var show_interactive_btn:bool = true
@export var stay_time:float = 0.5
@export var offest:Vector2
@export var interactive_btn: InteractiveBtn
var _sprite2d:Sprite2D
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
#endregion
#region 公共方法
func init_interactive_item(key:Key,sprite2D:Sprite2D):
	interactive_btn.init_interactive_btn(key,stay_time)
	interactive_btn.visible = show_interactive_btn
	self._sprite2d = sprite2D
func set_seleted(selectd:bool):
	_sprite2d.material.set_shader_parameter("enable",selectd)
	interactive_btn.visible = show_interactive_btn and selectd
	visible = selectd
func set_enable(enable:bool):
	visible = enable
	monitoring = enable
	monitorable = enable
func start_interact(entity):
	interactive_btn.start_interact(entity)
func end_interact(entity):
	interactive_btn.end_interact(entity)
#endregion
#region 私有方法
#endregion
#region 生命周期函数
func _ready() -> void:
	interactive_btn.position += offest
	area_entered.connect(func (area:Area2D):
		var entity = Util.get_entity_in_parent(area)
		if entity and entity == GamePlayerSystem.get_player() and not entity.interact_items.has(self):
			entity.interact_items.append(self)
			entity.interact_items.sort_custom(func (x,y):return (x.global_position - entity.global_position).length() < (y.global_position - entity.global_position).length())
		)
	area_exited.connect(func (area:Area2D):
		var entity = Util.get_entity_in_parent(area)
		if entity and entity == GamePlayerSystem.get_player():
			set_seleted(false)
			entity.interact_items.erase(self)
			entity.interact_items.sort_custom(func (x,y):return (x.global_position - entity.global_position).length() < (y.global_position - entity.global_position).length())
		)
#endregion
